using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.SupplyChain.Material;
using HengTong.Model;


namespace HengTong.ViewModel.SupplyChain.SysMaterialVms
{
    public partial class SysMaterialListVm : BaseListVm<SysMaterialView, SysMaterialSearcher>
    {

        protected override IEnumerable<IGridColumn<SysMaterialView>> InitGridHeader()
        {
            return new List<GridColumn<SysMaterialView>>{
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Code),
                this.MakeGridHeader(x => x.MaterialGroupName),
                this.MakeGridHeader(x => x.Description),
                this.MakeGridHeader(x => x.State),
                this.MakeGridHeader(x => x.UnitName),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysMaterialView> GetSearchQuery()
        {
            var query = DC.Set<SysMaterial>()
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.Code, x=>x.Code)
                .CheckEqual(Searcher.State, x=>x.State)
                .WhereIf(!string.IsNullOrWhiteSpace(Searcher.SkuName), x => x.Skus.Any(e => e.Name.Contains(Searcher.SkuName)))
                .WhereIf(!string.IsNullOrWhiteSpace(Searcher.SkuCode), x => x.Skus.Any(e => e.Code.Contains(Searcher.SkuCode)))
                .WhereIf(!string.IsNullOrWhiteSpace(Searcher.ErpId), x => x.Skus.Any(e => e.ErpId.Contains(Searcher.ErpId)))
                .WhereIf(!string.IsNullOrWhiteSpace(Searcher.ErpName), x => x.Skus.Any(e => e.ErpName.Contains(Searcher.ErpName)))
                .WhereIf(!string.IsNullOrWhiteSpace(Searcher.ErpModel), x => x.Skus.Any(e => e.ErpModel.Contains(Searcher.ErpModel)))
                .Select(x => new SysMaterialView
                {
				    ID = x.ID,
                    Name = x.Name,
                    Code = x.Code,
                    MaterialGroupName = x.MaterialGroup.Name,
                    Description = x.Description,
                    State = x.State,
                    UnitName = x.Unit.Name,
                    Remark = x.Remark,
                })
                .OrderBy(x => x.Code);
            return query;
        }
    }

    public class SysMaterialView : SysMaterial{
        [Display(Name = "简称")]
        public String MaterialGroupName { get; set; }
        [Display(Name = "名称")]
        public String UnitName { get; set; }

    }
}
